package com.hk.demo.player.preview;

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hk.demo.player.MyBaseActivity;
import com.hk.demo.player.R;
import com.hk.demo.player.http.ApiRepository;
import com.hk.demo.player.http.FirmMonitor;
import com.hk.player.PlayerControlView;
import com.hk.player.PlayerView;
import com.hk.player.core.DataCallback;
import com.zhoug.common.utils.ScreenOrientationHelper;
import com.zhoug.common.utils.ToastUtils;
import com.zhoug.zmui.group.ZMUIFlowViewGroup;
import com.zhoug.zmui.ratio.ZMUIRatioFrameLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;


public class PlayerActivity extends MyBaseActivity {
    private static final String TAG = ">>>PlayerActivity";
    private final List<FirmMonitor> mFirmMonitor = new ArrayList<>();
    private ScreenOrientationHelper mScreenOrientationHelper;
    private int mCurrentPlayIndex;
    private ApiRepository mApiRepository;
    private PlayerView mPlayerView;
    private TextView mBtnStartPlay;
    private TextView mBtnResumePlay;
    private TextView mBtnStopPlay;
    private TextView mBtnPausePlay;
    private TextView mBtnNextPlay;
    private TextView mBtnReleasePlayer;
    private PlayerControlView mPlayerControlView;
    private TextView mTvHeader;
    private ZMUIRatioFrameLayout mContainerVideo;
    private ZMUIFlowViewGroup mContainerBtn;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_player;
    }

    @Override
    protected void findViews() {
        mPlayerView = findViewById(R.id.playerView);
        mBtnStartPlay = findViewById(R.id.btn_startPlay);
        mBtnResumePlay = findViewById(R.id.btn_resumePlay);
        mBtnStopPlay = findViewById(R.id.btn_stopPlay);
        mBtnPausePlay = findViewById(R.id.btn_pausePlay);
        mBtnNextPlay = findViewById(R.id.btn_nextPlay);
        mBtnReleasePlayer = findViewById(R.id.btn_releasePlayer);
        mPlayerControlView = findViewById(R.id.playerControlView);
        mTvHeader = findViewById(R.id.tv_header);
        mContainerVideo = findViewById(R.id.container_Video);
        mContainerBtn = findViewById(R.id.container_btn);
    }

    @Override
    protected void onCreate1(Bundle savedInstanceState) {
        //切换横竖屏
        mScreenOrientationHelper=new ScreenOrientationHelper(this);
        mScreenOrientationHelper.setAutoFullScreen(true);
        mScreenOrientationHelper.setOnScreenOrientationListener(screenOrientation -> {
            if(screenOrientation==ScreenOrientationHelper.ORIENTATION_PORTRAIT){
                mTvHeader.setVisibility(View.VISIBLE);
                mContainerBtn.setVisibility(View.VISIBLE);
                ViewGroup.LayoutParams layoutParams = mContainerVideo.getLayoutParams();
                layoutParams.width= ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height= ViewGroup.LayoutParams.WRAP_CONTENT;
                mContainerVideo.setLayoutParams(layoutParams);
            } else if (screenOrientation==ScreenOrientationHelper.ORIENTATION_LANDSCAPE) {
                mTvHeader.setVisibility(View.GONE);
                mContainerBtn.setVisibility(View.VISIBLE);
                ViewGroup.LayoutParams layoutParams = mContainerVideo.getLayoutParams();
                layoutParams.width= ViewGroup.LayoutParams.MATCH_PARENT;
                layoutParams.height= ViewGroup.LayoutParams.MATCH_PARENT;
                mContainerVideo.setLayoutParams(layoutParams);
            }

        });
        mBtnStartPlay.setOnClickListener(v -> {
            mPlayerView.startPlay();
        });

        mBtnStopPlay.setOnClickListener(v -> {
            mPlayerView.stopPlay();
        });
        mBtnPausePlay.setOnClickListener(v -> {
            mPlayerView.pausePlay();
        });
        mBtnResumePlay.setOnClickListener(v -> {
            mPlayerView.startPlay();
        });
        mBtnReleasePlayer.setOnClickListener(v -> {
            mPlayerView.releasePlayer();
        });

        mBtnNextPlay.setOnClickListener(v -> {
            if (mCurrentPlayIndex < mFirmMonitor.size() - 1) {
                mCurrentPlayIndex++;
                mPlayerView.setMonitorData(mFirmMonitor.get(mCurrentPlayIndex).toMonitorData());
                mPlayerView.startPlay();
            }
        });



        mPlayerControlView.setBtnFlag(PlayerControlView.FLAG_RECORDING
                |PlayerControlView.FLAG_SCREEN_SHOT
                |PlayerControlView.FLAG_TALK
                |PlayerControlView.FLAG_WRITE
                |PlayerControlView.FLAG_PLAYBACK
                |PlayerControlView.FLAG_SOUND
                |PlayerControlView.FLAG_FULL_SCREEN
        );
        mPlayerControlView.setFullScreenBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               toastShort("点击全屏切换");

                mScreenOrientationHelper.changeScreenOrientation();
            }
        });
        mPlayerControlView.setPlaybackBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastShort("点击回放");
            }
        });
        mPlayerControlView.setRecordBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastShort("点击录像");
            }
        });
        mPlayerControlView.setQualityBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastShort("点击高清");
            }
        });
        mPlayerControlView.setScreenshotBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastShort("点击截图");
            }
        });
        mPlayerControlView.setMuteBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastShort("点击静音");
            }
        });
        mPlayerControlView.setTalkBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastShort("点击对讲");
            }
        });

        mPlayerControlView.setWriteBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastShort("点击巡检");
            }
        });

        loadData();
    }

    private void loadData(){
        mApiRepository = new ApiRepository();
        mApiRepository.getPreviewMonitorList(1, 100, new DataCallback<List<FirmMonitor>>() {
            @Override
            public void onSuccess(List<FirmMonitor> firmMonitors) {
                if (firmMonitors != null && !firmMonitors.isEmpty()) {
                    mFirmMonitor.addAll(firmMonitors);
                    mCurrentPlayIndex = 0;
                    mPlayerView.setMonitorData(mFirmMonitor.get(mCurrentPlayIndex).toMonitorData());
                } else {
                    ToastUtils.toastLong("没有获取到摄像头");
                }

            }

            @Override
            public void onFailure(String error) {
                ToastUtils.toastLong("获取摄像头失败:" + error);
            }
        });
    }


    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mScreenOrientationHelper.onConfigurationChanged(newConfig );
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPlayerView.releasePlayer();
    }

}